﻿CREATE VIEW [Messenger].[V_UtentiConnessi] AS 
-- Utenti connessi
WITH PingValidi(UserName) AS
(
	SELECT DISTINCT P.Username
	FROM [Messenger].Ping P
	WHERE P.TimeStamp_Insert > 
		(
			SELECT TOP 1 DATEADD(SECOND, -C.FinestraOnlineSecondi, GETDATE())
			FROM [Messenger].Configurazione C
		)
)
SELECT U.Username, CAST(CASE WHEN PV.Username IS NULL THEN 0 ELSE 1 END AS bit) AS IsOnline
	FROM PingValidi PV
	RIGHT JOIN [Utenti].Utente U
		ON PV.Username = U.Username